home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 513 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need help with QSORT
  5. Date: Sat, 06 Jan 96 13:54:37 GMT
  6. Organization: none
  7. Message-ID: <820936477snz@genesis.demon.co.uk>
  8. References: <4ckjfi$oat@swifty.cfa.org> <4cl0rb$2id@news.iag.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4cl0rb$2id@news.iag.net> jatmon@iag.net "John R Buchan" writes:
  15.  
  16. ...
  17.  
  18. >int sort_function( const void *c, const void *d)
  19. >   {
  20. >   float f;
  21. >   const struct site_specs *a = c, *b = d; /* jatmon added const */
  22. >
  23. >/*   a =(struct site_specs *) c;  the casts are unnecessary with const */
  24. >/*   b =(struct site_specs *)d;                                        */
  25. >
  26. >/*
  27. >   if((a->XC == b->XC) && (a->YC == b->YC) && (a->ZC == b->ZC))
  28. >      return(0);
  29. >
  30. >   if(a->ZC < b->ZC)
  31. >      return(-1);
  32. >   if(a->ZC > b->ZC)
  33. >      return(1);
  34. >
  35. >   if(a->YC < b->YC)
  36. >      return(-1);
  37. >   if(a->YC > b->YC)
  38. >      return(1);
  39. >
  40. >   if(a->XC < b->XC)
  41. >      return(-1);
  42. >   if(a->XC > b->XC)
  43. >      return(1);
  44. >
  45. >The return values don't have to be -1, 0, 1.  Just negative, 0, positive.
  46. >
  47.  
  48. >Comparing floats is a bit tricky.
  49.  
  50. It can be if you're testing for equality in some sense. As far as sorting
  51. goes all you really need is a well-defined ordering and float comparison
  52. should be fine for that. However comparison isn't the only thing that is
  53. 'tricky' about floating point.
  54.  
  55.   The c.l.c faq (Frequently Asked Question)
  56. >list explains the problems involved.  The list is available for anonymous ftp
  57. >from rtfm.mit.edu /pub/usenet/comp.lang.c.
  58. >*/
  59. >   f = a->ZC - b->ZC;
  60.  
  61. This may overflow. There is also the issue whether it will produce the
  62. same ordering results (where equality is concerned) as a->ZC (>,=,<) b->ZC.
  63. If not this may prove inconsistent with other parts of the code and
  64. cause problems.
  65.  
  66. >   if( f == 0)
  67. >      {
  68. >      f = a->YC - b->YC;
  69. >      if( f == 0)
  70. >         {
  71. >         f = a->XC - b->XC;
  72. >         }
  73. >      }
  74. >
  75. >   return f;
  76.  
  77. Converting a float to int could very easily overflow or truncate to zero so
  78. you do need to do something like return -1, 0, 1 explicitly.
  79.  
  80. I suggest something like the following:
  81.  
  82. int sort_function( const void *c, const void *d)
  83.    {
  84.    const struct site_specs *a = c, *b = d;
  85.  
  86.    if (a->ZC != b->ZC)
  87.       return (a->ZC > b->ZC) ? 1 : -1;
  88.  
  89.    if (a->YC != b->YC)
  90.       return (a->YC > b->YC) ? 1 : -1;
  91.  
  92.    if (a->XC != b->XC)
  93.       return (a->XC > b->XC) ? 1 : -1;
  94.  
  95.    return 0;
  96.    }
  97.  
  98. -- 
  99. -----------------------------------------
  100. Lawrence Kirby | fred@genesis.demon.co.uk
  101. Wilts, England | 70734.126@compuserve.com
  102. -----------------------------------------
  103.